import chartWrap from '../../asset/js/canvas/chartWrap'
import getConfig from './getConfig'
Page({
    data:{
        loading:true,
        max_heart: getApp().globalData.UserData.max_heart,
        id: getApp().globalData.UserData .id,
        detail:{
            avg_heart: 0,//平均心率
            cost_time: 0,//运动耗时
            calorie: 0,//卡路里
            max_heart: 0,//最大心率
            level_1: 1,//热身占比
            level_2: 1,//减脂占比
            level_3: 1,//糖元消耗占比
            level_4: 1,//乳酸堆积
            level_5: 1,//极限运动占比
            user_id: 0,//用户id
            mac_addr: "22:22:33:44:55:66",//设备地址
            create_time: "2017-04-25 17:37:01",
            update_time: "2017-04-25 17:37:01"
        },
        max_hearttxt:'',
        avg_hearttxt:'',
        date:'',
        
    },onLoad(option){
        let now =new Date();
        let nowStr = option.date||now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
        this.setData({
            date:nowStr
        })
    },
    onShow(){
      this.setData({
        max_heart: getApp().globalData.UserData.max_heart,
        id: getApp().globalData.UserData.id
      })
    },
    onShareAppMessage(){
      return {
        title: '我的运动记录',
        path: '/daydetail/daydetail?id='+this.data.id,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    },
    onReady:function(){
        let self =this;
        getApp().request({
            url:'heart_record_list',
            data:{
                date:'2017-04-25'
            },success(res){
                let dt = res.data;
                let max = dt.data[0].max_heart;
                let avg = dt.data[0].avg_heart;
                let oldmax = self.data.max_heart;
                let maxper = Math.floor(max*100/oldmax)
                let avgper = Math.floor(avg*100/oldmax)
                if(dt.flag){
                    self.setData({detail:dt.data[0],max_hearttxt:max+"/"+maxper,avg_hearttxt:avg+"/"+avgper})
                    self.init();
                }else{

                }

            },fail(){
                wx.showModal({
                    content: '请求失败',
                    showCancel:false,
                    success: function(res) {
                    }
                })
            },complete(){
                self.setData({loading:false})
            }
        })

    },
    init(){
        let partcial = this.data.detail;
        let canvasId='myCanvas'
        let canvasConfig={
            width:220,
            height:220,
            id:canvasId
        }
        let label=[
                "热身 "+partcial.level_1*100+"%",
                "燃脂 "+partcial.level_2*100+"%",
                "糖原消耗 "+partcial.level_3*100+"%",
                "乳酸堆积 "+partcial.level_4*100+"%",
                "极限运动 "+partcial.level_5*100+"%"
            ];
        let data=[
            partcial.level_1,partcial.level_2,partcial.level_3,partcial.level_4,partcial.level_5
        ]
        let config=getConfig(canvasConfig,label,data)
        chartWrap.bind(this)(config)
    }
})